#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(TEST_TARGET_str)
#include <varch/command.h>
#include <varch/unitt.h>
#include <varch/str.h>
#else  
#include "init.h"
#include "command.h"
#include "unitt.h"
#include "kern.h"
#include "str.h"
#endif

/************************************************************************************/
/************************************* Unit Test ************************************/
/************************************************************************************/

// #define EXIT_TEST
extern uint64_t unitt_clock(void);

static int test_0(void)
{
    for (int i = 0; i < 100; i++)
    {
        if (0) 
        {
            
            #if defined (EXIT_TEST)
            exit(0);
            #endif 
            return UNITT_E_FAIL;
        }
    }
    
    return UNITT_E_OK;
}

static void unitt_task(void)
{
    static UNITT_TCASE rand_tests[] = {
        UNITT_TCASE(test_0),
        // UNITT_TCASE(test_1),
        // UNITT_TCASE(test_2),
    };

    static UNITT suites[] = {
        { "xxx suite", rand_tests, sizeof(rand_tests) / sizeof(rand_tests[0]) , unitt_clock },
    };

    UNITT_EXE(suites);
}

/************************************************************************************/
/************************************* Base Test ************************************/
/************************************************************************************/

static void test_create(void)
{
    str_t ss = str("Hello str!"); // 用数组字符串进行赋值构造
    str_t copy = str(ss); // 用str字符串进行拷贝构造
    _str(ss); // 删除str
    _str(copy); // 删除str
}

static void test_c_str(void)
{
    str_t ss = str("Hello str!"); // 用数组字符串进行赋值构造
    str_t copy = str(ss); // 用str字符串进行拷贝构造
    printf("ss: %s\r\n", _S(ss));
    printf("copy: %s\r\n", _S(copy));
    _str(ss); // 删除str
    _str(copy); // 删除str
}

static void test_assign(void)
{
    str_t ss = str(""); // 初始化为空字符串
    printf("ss: %s\r\n", _S(ss));
    str_assign(ss, "Hello str!"); // 赋值为 Hello str!
    printf("ss: %s\r\n", _S(ss));
    str_assign(ss, "This is the assignment method!"); // 再赋值为 This is the assignment method!
    printf("ss: %s\r\n", _S(ss));
    _str(ss);
}

static void test_append(void)
{
    str_t name = str("123456789");
    str_t ident = str("qq");
    str_t email = str("");

    str_append(email, name, "@", ident, ".com"); // 拼接若干个字符串
    printf("%s\r\n", str_c_str(email));

    _str(name);
    _str(ident);
    _str(email);
}

static void test_insert(void)
{
    str_t ss = str("0123456");
    str_insert(ss, 3, "|insert|"); // 在位置3插入字符串
    printf("ss: %s\r\n", str_c_str(ss));
    _str(ss);
}

static void test_erase(void)
{
    str_t ss = str("0123456789");
    str_erase(ss, 3, 3); // 移除位置3后面三个字符，也就是移除 345
    printf("ss: %s\r\n", str_c_str(ss));
    _str(ss);
}

static void test_op_end(void)
{
    str_t ss = str("0123456789");
    str_push_back(ss, 'A');
    printf("ss: %s\r\n", str_c_str(ss));
    printf("pop %c\r\n", str_pop_back(ss));
    printf("ss: %s\r\n", str_c_str(ss));
    _str(ss);
}

static void test_compare(void)
{
    str_t ss = str("01234");
    str_t copy = str(ss);
    printf("compare: %d\r\n", str_compare(ss, copy));
    printf("compare: %d\r\n", str_compare(ss, "01233"));
    printf("compare: %d\r\n", str_compare(ss, "012345"));
    _str(ss);
    _str(copy);
}

static void test_substr(void)
{
    str_t ss = str("0123456789");
    str_t sub = str_substr(ss, 4, 2);
    printf("ss: %s\r\n", str_c_str(ss));
    printf("sub: %s\r\n", str_c_str(sub));
    _str(ss);
    _str(sub);
}

static void test_at(void)
{
    str_t ss = str("0123456789");
    printf("ss[3] = %c\r\n", str_at(ss, 3));
    str_at(ss, 3) = 'A';
    printf("ss: %s\r\n", _S(ss));
    _str(ss);
}

static void test_find(void)
{
    str_t ss = str("0123456789");
    printf("find = %d\r\n", str_find(ss, "3456", 0)); // 从0位置开始找
    printf("rfind = %d\r\n", str_rfind(ss, "23", str_length(ss) - 1)); // 从尾端开始往回找
    printf("find = %d\r\n", str_find(ss, "0000", 0)); // 找一个不存在的字符串
    _str(ss);
}

static void test_find_of(void)
{
    str_t ss = str("C:/workspace/project/C/00 - vlib/Project/main.c"); // 先给定一个文件路径
    str_t pan = NULL; // 初始化为NULL，不构建
    str_t filename = NULL; // 初始化为NULL，不构建
    pan = str_substr(ss, 0, str_find_first_of(ss, ":", 0)); // 先找到第一个 :
    filename = str_substr(ss, str_find_last_of(ss, "\\/", str_length(ss)-1) + 1, str_length(ss)); // 找到最后一个 \ 或者 / 路径分隔符
    printf("pan: %s\r\n", str_c_str(pan));
    printf("filename: %s\r\n", str_c_str(filename));
    _str(ss);
    _str(pan);
    _str(filename);
}

static void test_reverse(void)
{
    str_t ss = str("0123456789");
    str_reverse(ss, 2, 8);
    printf("ss = %s\r\n", str_c_str(ss));
    _str(ss);
}

static void test_replace(void)
{
    str_t ss = str("My name is ZhangSan!");
    printf("ss = %s\r\n", str_c_str(ss));
    str_replace(ss, 11, 8, "Lisi"); // 把ZhangSan替换成Lisi
    printf("ss = %s\r\n", str_c_str(ss));
    _str(ss);
}

static void test_copy(void)
{
    str_t ss = str("0123456789");
    char array[32] = { 0 };
    int length = 0;
    length = str_copy(ss, 5, sizeof(array), array); // 从位置开始复制，复制最大长度为数组的长度
    array[length] = '\0'; // 在后面添加结束符，方便打印
    printf("length = %d, copy: %s\r\n", length, array);
    _str(ss);
}

static void test_format(void)
{
    str_t ss = str("");
    str_t pp = str("format");
    str_format(ss, "Hello str! %s %s! int:%d, float:%.2f, char:%c", pp, "function", 18, 175.5, 'A');
    printf("ss: %s\r\n", str_c_str(ss));
    _str(ss);
    _str(pp);
}

static void test_base(void)
{
    str_t s = str("0123456789");
    str_replace(s, 9, 10, "AAAAAAAAAA");
    printf("len %d, %d\r\n", str_length(s), str_capacity(s));
    printf("%s\r\n", _S(s));
    _str(s);
}

/************************************************************************************/
/*************************************  Command  ************************************/
/************************************************************************************/

static void usage(void)
{
    printf(
"Usage: str [opt] [arg] ...\n"
"\n"
"options:\n"
"    -e <execute>        Specifies the function to execute, the default is the <base> test\n"
"                        <base>      Test base function\n"
"                        <ut>        Unit test\n"
"                        <create>    Test create and delete functions\n"
"                        <cstr>      Test get c string function\n"
"                        <insert>    Test insert function\n"
"                        <erase>     Test erase function\n"
"                        <assign>    Test assign function\n"
"                        <append>    Test append function\n"
"                        <opend>     Test operate end (push, pop) function\n"
"                        <compare>   Test compare function\n"
"                        <substr>    Test create sub-string function\n"
"                        <at>        Test get data function\n"
"                        <find>      Test find function\n"
"                        <findof>    Test find-of function\n"
"                        <reverse>   Test reverse function\n"
"                        <replace>   Test replace function\n"
"                        <copy>      Test copy function\n"
"                        <format>    Test format function\n"
"    -h                  Print help\n"
"    -v                  Print version\n"
"    -u [<period>]       Unit test period, unit ms, the default is 1000ms\n"
"\n"

    );
}

static int test(int argc, char *argv[])
{
    char *execute = NULL;
    int ut_period = 1000;

    /* reset getopt */
    command_opt_init();

    while (1)
    {
        int opt = command_getopt(argc, argv, "e:hvu::");
        if (opt == -1) break;

        switch (opt) 
        {
        case 'u' :
            if (command_optarg) ut_period = atoi(command_optarg);
            break;
        case 'e' :
            execute = command_optarg;
            break;
        case 'v' :
            printf("str version %d.%d.%d\r\n", STR_V_MAJOR, STR_V_MINOR, STR_V_PATCH);
            return 0;
        case '?':
            printf("Unknown option `%c`\r\n", command_optopt);
            return -1;
        case 'h' : 
        default:
            usage();
            return 0;
        }
    }

    if (execute)
    {
        if (!strcmp(execute, "base"))
        {
            test_base();
        }
        else if (!strcmp(execute, "ut"))
        {
            srand((unsigned int)time(NULL));
            #if defined(TEST_TARGET_str)
            while (1)
            {
                unitt_task();
                usleep(1000 * ut_period);
            }
            #else  
            printf("create task %d\r\n", task_create(ut_period, unitt_task));
            #endif
        }
        else if (!strcmp(execute, "create"))
        {
            test_create();
        }
        else if (!strcmp(execute, "cstr"))
        {
            test_c_str();
        }
        else if (!strcmp(execute, "assign"))
        {
            test_assign();
        }
        else if (!strcmp(execute, "append"))
        {
            test_append();
        }
        else if (!strcmp(execute, "insert"))
        {
            test_insert();
        }
        else if (!strcmp(execute, "erase"))
        {
            test_erase();
        }
        else if (!strcmp(execute, "opend"))
        {
            test_op_end();
        }
        else if (!strcmp(execute, "compare"))
        {
            test_compare();
        }
        else if (!strcmp(execute, "substr"))
        {
            test_substr();
        }
        else if (!strcmp(execute, "at"))
        {
            test_at();
        }
        else if (!strcmp(execute, "find"))
        {
            test_find();
        }
        else if (!strcmp(execute, "findof"))
        {
            test_find_of();
        }
        else if (!strcmp(execute, "reverse"))
        {
            test_reverse();
        }
        else if (!strcmp(execute, "replace"))
        {
            test_replace();
        }
        else if (!strcmp(execute, "copy"))
        {
            test_copy();
        }
        else if (!strcmp(execute, "format"))
        {
            test_format();
        }
    }
    else  
    {
        test_base();
    }
    
    return 0;
}

/************************************************************************************/
/************************************ Test entry ************************************/
/************************************************************************************/

#if defined(TEST_TARGET_str)
int main(int argc, char *argv[])
{
    return test(argc, argv);
}
#else 
void test_str(void)
{
    command_export("str", test);
}
init_export_app(test_str);
#endif 
